package com.dcy.znoa.service.publiz.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.dao.publiz.CameraDao;
import com.dcy.znoa.dao.publiz.CompanyDao;
import com.dcy.znoa.dao.publiz.PlaceDao;
import com.dcy.znoa.domain.publiz.Camera;
import com.dcy.znoa.domain.publiz.Company;
import com.dcy.znoa.domain.publiz.Place;
import com.dcy.znoa.service.publiz.PlaceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author pengyu
 */
@Slf4j
@Service
public class PlaceServiceImpl implements PlaceService {

    @Autowired
    PlaceDao placeDao;
    @Autowired
    CameraDao cameraDao;
    @Autowired
    CompanyDao companyDao;

    @Autowired
    ThreadPoolTaskExecutor defaultExecutor;

    @Override
    public Place selectTreeByParkId(Long parkId) {
        //暂时不考虑写缓存，没心情    先查询缓存,从redis中查询
//        JsonForList json = getJsonListfromRedisOfJson(COMP_DEP_PREFIX + String.valueOf(compId));
//        if (json != null) {
//            return json;
//        }
        updateRedis(parkId);
        //查询园区id对应下的 场所根节点 对应的 placeId
        List<Place> list = placeDao.selectList(new EntityWrapper<Place>().eq("entity_id", parkId).eq("place_type", 1).eq("place_level", 1));
        if (list == null || list.isEmpty()) {
            return null;
        }
//        List<Place> resultList = new ArrayList<>();
        Place place = list.get(0);
//        Long placeId = place.getPlaceId();
        querySubPlaceinfo(place);
        return place;
    }

    @Override
    public Place selectTreeByPlaceId(Long placeId) {
        //不查询缓存
        Place place = placeDao.selectById(placeId);
        if (place == null) {
            return null;
        }
        querySubPlaceinfo(place);
        return place;
    }

    private void querySubPlaceinfo(Place place) {
        //查询下级场所
        Long placeId = place.getPlaceId();
        List<Place> subPlaceList = placeDao.selectList(new EntityWrapper<Place>().eq("p_place_id", placeId));
        if (!subPlaceList.isEmpty()) {
            for (Place subPlace : subPlaceList) {
                querySubPlaceinfo(subPlace);
            }
            place.setSubPlaceList(subPlaceList);
        }
    }
    //查询下级场所并标记是否可编辑(园区)
    private void querySubPlaceinfoCanEditforPark(Place place) {
        Long placeId = place.getPlaceId();
        List<Place> subPlaceList = placeDao.selectList(new EntityWrapper<Place>().eq("p_place_id", placeId));
        if (!subPlaceList.isEmpty()) {
            for (Place subPlace : subPlaceList) {
                if(Integer.valueOf(1).equals(subPlace.getPlaceType())){//如果是园区类型的场所 ，就可以编辑
                    subPlace.setCanEdit(1);
                }
                querySubPlaceinfoCanEditforPark(subPlace);
            }
            place.setSubPlaceList(subPlaceList);
        }
    }

    //查询下级场所并标记是否可编辑(园区)
    private void querySubPlaceinfoCanEditforComp(Long compId , Place place) {
        Long placeId = place.getPlaceId();
        List<Place> subPlaceList = placeDao.selectList(new EntityWrapper<Place>().eq("p_place_id", placeId));
        List<Place> listForInsert = new ArrayList<>();
        if (!subPlaceList.isEmpty()) {
            for (Place subPlace : subPlaceList) {
                //如果是公司类型的场所 并且 对应的公司和入参的公司ID一致 ，就可以编辑
                if(Integer.valueOf(2).equals(subPlace.getPlaceType()) && compId.equals(subPlace.getEntityId())){
                    subPlace.setCanEdit(1);
                    listForInsert.add(subPlace);//本公司的可见
                }else if(Integer.valueOf(1).equals(subPlace.getPlaceType())){
                    listForInsert.add(subPlace); //园区的可见
                }
                querySubPlaceinfoCanEditforComp(compId,subPlace);
            }
            place.setSubPlaceList(listForInsert);//subPlaceList
        }
    }

    @Override
    public void updateRedis(Long parkId) {

    }


    @Override
    public Integer insert(Place var1) throws BusinessException {
        return placeDao.insert(var1);
    }

    @Override
    public Integer updateById(Place var1) throws BusinessException {
        return placeDao.updateById(var1);
    }

    @Override
    public Integer deleteById(Long placeId) {
        Place place = placeDao.selectById(placeId);
        //查不到，当然就不能删除
        if (place == null) {
            return -99;
        }
        //1 有下级场所的不允许删除
        Integer subPlaceCount = placeDao.selectCount(new EntityWrapper<Place>().eq("p_place_id", placeId));
        if (subPlaceCount > 0) {
            return -1;
        }
        //2 有关联视频的场所 不允许删除
        Integer cameraCount = cameraDao.selectCount(new EntityWrapper<Camera>().eq("entity_type", 5).eq("entity_id", placeId));
        if (cameraCount > 0) {
            return -2;
        }
        Integer placeType = place.getPlaceType();
        if (placeType == null) {//没有场所类型，数据本身完整性有误，不能删除
            return -98;
        }
        Integer placeLevel = place.getPlaceLevel();
        //3     1 园区 2 公司 3 食堂 4 停车场 对应的场所不能删除
        switch (placeType){//场所归属类型：1 园区 2 公司 3 食堂 4 停车场 99 其他，不能留空
            case 1:
                if(placeLevel<=1){
                    return -3;
                }
               break;
            case 2:
                if(placeLevel<=2){
                    return -4;
                }
                break;
            case 3:
                return -5;
            case 4:
                return -6;
            default:
                break;
        }
        return placeDao.deleteById(placeId);
    }

    @Override
    public Place selectSeeTreeByParkId(long parkId) {
        updateRedis(parkId);
        //查询园区id对应下的 场所根节点 对应的 placeId
        List<Place> list = placeDao.selectList(new EntityWrapper<Place>().eq("entity_id", parkId).eq("place_type", 1).eq("place_level", 1));
        if (list == null || list.isEmpty()) {
            return null;
        }
//        List<Place> resultList = new ArrayList<>();
        Place place = list.get(0);
//        Long placeId = place.getPlaceId();
        querySubPlaceinfoCanEditforPark(place);
        return place;
    }

    @Override
    public Place selectSeeTreeByCompId(Long compId) {
//        updateRedis(compId);
        //查询园区id对应下的 场所根节点 对应的 placeId 写死为1L
        Company company = companyDao.selectById(compId);
        Long placeId = company.getParkId();
        if(placeId==null){
            placeId = 1L;
        }
        List<Place> list = placeDao.selectList(new EntityWrapper<Place>().eq("entity_id", placeId).eq("place_type", 1).eq("place_level", 1));
        if (list == null || list.isEmpty()) {
            return null;
        }
//        List<Place> resultList = new ArrayList<>();
        Place place = list.get(0);
//        Long placeId = place.getPlaceId();
        querySubPlaceinfoCanEditforComp(compId,place);
        return place;
    }

    @Override
    public void updateRedis(Integer parkId) {

    }


}